package com.bw.user.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bw.user.entity.PageResult;
import com.bw.user.mapper.TUserMapper;
import com.bw.user.pojo.TUser;
import com.bw.user.pojo.TUserExample;
import com.bw.user.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    TUserMapper tUserMapper;

    @Override
    public PageResult findPage(int page, int rows, TUser tUser) {
        PageHelper.startPage(page,rows);
        String uname = tUser.getUname();
        TUserExample example = new TUserExample();
        if(!StringUtils.isEmpty(uname)){
            example.createCriteria().andUnameLike("%"+tUser.getUname()+"%");
        }
        Page<TUser> page1 = (Page<TUser>)tUserMapper.selectByExample(example);
        return new PageResult(page1.getTotal(),page1.getResult());
    }

    @Override
    public void save(TUser tUser) {
        tUserMapper.insert(tUser);
    }

    @Override
    public TUser edit(int uid) {
        return tUserMapper.selectByPrimaryKey(uid);
    }

    @Override
    public void update(TUser tUser) {
        tUserMapper.updateByPrimaryKeySelective(tUser);
    }

    @Override
    public void dele(int[] ids) {
        if(ids!=null && ids.length>0){
            for(int uid : ids){
                tUserMapper.deleteByPrimaryKey(uid);
            }
        }
    }


}
